// pages/charge/c_record/c_record.js

const app = getApp() // 获取应用实例
const util = require('../../../utils/util.js')
import { getRecordList } from '../../../utils/http/api'

Page({
  // 缴费记录页面的初始数据
  data: {
    /**缴费记录列表数据  数据格式
     * [{id:'',name:'',amount_total:'',group:{id:'',type:'',name:''}},{}]
     */
    record_list: [],
  },

  // onRecordClick 查看缴费详情
  onRecordClick: function (e) {
    let index = e.currentTarget.dataset.index
    let id = this.data.record_list[index].id
    let url = '/pages/charge/c_reco_detail/c_reco_detail?id=' + id
    let type = this.data.record_list[index].type;// 0 房租 1 水 2 电 3 停车
    if (type == '0') {
      url = '/pages/charge/c_reco_detail_2/c_reco_detail_2?id=' + id
    } else if (type == '1') {
      url = '/pages/charge/c_reco_detail/c_reco_detail?id=' + id + '&type=1'
    } else if (type == '2') {
      url = '/pages/charge/c_reco_detail/c_reco_detail?id=' + id + '&type=2'
    }
    console.log('url = ' + url)
    util.navigateTo(url)
  },

  // 请求我的缴费列表
  async reqRecordList() {
    wx.showLoading({ title: '处理中', mask: true, })
    let { code, msg, data } = await getRecordList({ page: '1', limit: '999999', token: app.gl.token })
    console.log(`reqRecordList...code=${code},msg=${msg},data=`, data)
    wx.stopPullDownRefresh()
    if (code == '1') {
      this.setData({ record_list: data.data });
    } else {
      wx.showToast({ title: msg, icon: 'error' })
    }
  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    this.reqRecordList()
  },

  // 生命周期函数--监听页面显示
  onShow: function () {

  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.reqRecordList()
  },

})